from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from try_mysql import *
from PyQt5.uic import loadUiType
from movie_record_manage_ui import Ui_Movie_record
from movie_manage import Movie_manage_App
from movie_record_manage import Movie_record_manage_App
from sale_record import Sale_Record_App
import movie_record_manage
import sale_record
import sys

ui, _ = loadUiType('cinema.ui')

class Cinema_App(QMainWindow, ui):
    #定义构造
    def __init__(self):
        QMainWindow.__init__(self)
        self.setupUi(self)
        self.handle_ui_change()
        self.handle_buttons()
        self.show_tab1()
        self.show()

    def handle_ui_change(self):
        self.tabWidget.tabBar().setVisible(False)


    def handle_buttons(self):
        self.sale_ticket_record.clicked.connect(self.Open_sale_ticket_record)
        self.ticket_sale_query.clicked.connect(self.Query_ticket_query)
        self.add_ticket_record.clicked.connect(self.Add_ticket_record)
        self.drop_ticket_record.clicked.connect(self.Drop_ticket_record)
        self.sale_record.clicked.connect(self.Sale_record)
        self.movie_manage.clicked.connect(self.Movie_manage)
        self.movie_record.clicked.connect(self.Movie_record)
        self.staff_manage.clicked.connect(self.Open_staff_manage)
        self.query_staff_name.clicked.connect(self.Query_staff_manage)

    def Open_sale_ticket_record(self):
        self.tabWidget.setCurrentIndex(0)

    def Query_ticket_query(self):
        conn = get_conn()
        cur = conn.cursor()
        Number=self.query_number.text()
        MovieName=self.query_movie_name.text()
        Date=self.query_date.text()
        sql = "select * from tickets_manage where Number=%s or MovieName=%s or Date=%s"
        cur.execute(sql,(Number,MovieName,Date))
        data = cur.fetchall()
        if data:
            self.query_ticket_record_tab.setRowCount(0)
            self.query_ticket_record_tab.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.query_ticket_record_tab.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1
                row_position = self.query_ticket_record_tab.rowCount()
                self.query_ticket_record_tab.insertRow(row_position)
        else:
            self.statusBar().showMessage('没有你要的结果。。')


    def Add_ticket_record(self):
        conn = get_conn()
        cur = conn.cursor()
        Number = self.number.text()
        MovieName = self.movie_name.text()
        Seat = self.seat.text()
        TicketNum = self.ticket_number.text()
        Date = self.date.text()
        Plan = self.plan.text()
        MovieKind = self.kind_movie.currentText()
        Price = self.price.text()
        if (MovieName =="" or Seat =="" or TicketNum=="" or Date=="" or Plan=="" or Price==""):
            self.statusBar().showMessage('字段为空添加有毛用！')
            return
        else:
            sql = "insert into tickets_manage(Number,MovieName, Seat, TicketNum, Date, Plan, MovieKind, Price) VALUES (%s,%s,%s,%s,%s,%s,%s,%s)"
            values = (Number, MovieName, Seat, TicketNum, Date, Plan, MovieKind, Price)
            cur.execute(sql, values)
            conn.commit()
            close_conn(conn, cur)
            self.statusBar().showMessage('好耶！添加成功!')
            self.show_tab1()
            self.clearEdit()
        return

    def Drop_ticket_record(self):
        conn = get_conn()
        cur = conn.cursor()
        Number = self.number.text()
        MovieName=self.movie_name.text()
        if (Number==""):
            self.statusBar().showMessage('字段为空你要删啥啊！')
            return
        else:
            sql1 = "delete from tickets_manage where Number=%s and MovieName=%s"
            cur.execute(sql1, (Number,MovieName ))
            conn.commit()
            close_conn(conn, cur)
            self.statusBar().showMessage('你真棒还把爷删了。。。')
            self.show_tab1()
            self.clearEdit()
        return



    def Sale_record(self):
        self.Mainwindow=Sale_Record_App()

    def Movie_manage(self):
        self.Mainwindow=Movie_manage_App()

    def Movie_record(self):
        self.Mainwindow=Movie_record_manage_App()

    def Open_staff_manage(self):
        self.tabWidget.setCurrentIndex(1)

    def Query_staff_manage(self):
        conn = get_conn()
        cur = conn.cursor()
        StaffName = self.staff_name.text()
        sql = "select * from manage_staff where StaffName=%s"
        cur.execute(sql, (StaffName,))
        data = cur.fetchall()
        if data:
            self.staff_tab.setRowCount(0)
            self.staff_tab.insertRow(0)
            for row, form in enumerate(data):
                for column, item in enumerate(form):
                    self.staff_tab.setItem(row, column, QTableWidgetItem(str(item)))
                    column += 1
                row_position = self.staff_tab.rowCount()
                self.staff_tab.insertRow(row_position)
        else:
            self.statusBar().showMessage('没有这个人。。')


    def show_tab1(self):
        conn=get_conn()
        cur=conn.cursor()
        sql="select * from tickets_manage"
        cur.execute(sql,)
        data = cur.fetchall()
        if data:
            self.manage_record_tab.setRowCount(0)
            self.manage_record_tab.insertRow(0)
            for row , form in enumerate(data):
                for column ,item in enumerate(form):
                    self.manage_record_tab.setItem(row,column,QTableWidgetItem(str(item)))
                    column += 1
                row_position=self.manage_record_tab.rowCount()
                self.manage_record_tab.insertRow(row_position)


    def clearEdit(self):
        self.number.clear()
        self.movie_name.clear()
        self.seat.clear()
        self.ticket_number.clear()
        self.date.clear()
        self.plan.clear()
        self.price.clear()





def main():
    app = QApplication([])
    window = Cinema_App()
    window.show()
    app.exec_()

if __name__ == '__main__':
    main()